package com.xinjiang.electricpower.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.PageDomain;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.core.web.page.TableSupport;
import com.xinjiang.electricpower.domain.DTO.WorkOrderIndexDto;
import com.xinjiang.electricpower.domain.vo.TodoOrderInfoVo;
import com.xinjiang.electricpower.service.IWorkOrderIndexService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhanghui
 * @date 2023/2/11 9:59
 * @Description
 */
@RestController
@RequestMapping("/xinjiang-power-supply-station")
@Api(value = "工单首页提供外部接口", tags = "工单首页提供外部接口")
public class WorkOrderIndexController extends BaseController {

    @Autowired
    private IWorkOrderIndexService workOrderIndexService;

    @GetMapping("/index/todo-order")
    @ApiOperation(value = "查询首页待办工单", notes = "查询首页待办工单")
    public TableDataInfo getXjOrderTodo(WorkOrderIndexDto workOrderIndexDto) {
//        startPage();
        List<TodoOrderInfoVo> list = workOrderIndexService.getTodoOrderInfo(workOrderIndexDto);
        //手动进行分页
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("请求成功");
        if (ObjectUtils.isEmpty(list)) {
            tableDataInfo.setTotal(0);
            tableDataInfo.setRows(null);
            return tableDataInfo;
        }
        int total = list.size();
        List<TodoOrderInfoVo> newList = list.stream().skip((long) (pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        tableDataInfo.setTotal(total);
        tableDataInfo.setRows(newList);
        return tableDataInfo;
    }


    @GetMapping("/index/yj-order")
    @ApiOperation(value = "查询首页预警工单", notes = "查询首页预警工单")
    public R<Map<String, Object>> getXjOrderYj(WorkOrderIndexDto workOrderIndexDto) {
        Map<String, Object> alertWorkOrderNum = workOrderIndexService.getAlertWorkOrderNum(workOrderIndexDto);
        return R.ok(alertWorkOrderNum, "查询成功");
    }

}
